﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("ImageUrl"), _
ToolboxData("<{0}:Uploader runat=server></{0}:Uploader>"), _
ToolboxBitmap(GetType(Thumb), "UploaderThumb.png")> _
Public Class Uploader
    Inherits WebControl

#Region "Constants"

#End Region

#Region "Properties"

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True), UrlProperty(), _
    Editor(GetType(System.Web.UI.Design.UrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Property FlashFilePath() As String
        Get
            If IsNothing(ViewState("FlashFilePath")) Then
                Return String.Empty
            Else
                Return CStr(ViewState("FlashFilePath"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("FlashFilePath") = Value
        End Set
    End Property

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True), UrlProperty(), _
    Editor(GetType(System.Web.UI.Design.UrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Property UploadFolder() As String
        Get
            If IsNothing(ViewState("UploadFolder")) Then
                Return String.Empty
            Else
                Return CStr(ViewState("UploadFolder"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("UploadFolder") = Value
        End Set
    End Property

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True)> _
    Property FlashWidth() As Integer
        Get
            If IsNothing(ViewState("FlashWidth")) Then
                Return 600
            Else
                Return CInt(ViewState("FlashWidth"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("FlashWidth") = Value
        End Set
    End Property

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True)> _
    Property FlashHeight() As Integer
        Get
            If IsNothing(ViewState("FlashHeight")) Then
                Return 400
            Else
                Return CInt(ViewState("FlashHeight"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("FlashHeight") = Value
        End Set
    End Property

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True)> _
    Property MaxFileSize() As Integer
        Get
            If IsNothing(ViewState("MaxFileSize")) Then
                Return 100
            Else
                Return CInt(ViewState("MaxFileSize"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("MaxFileSize") = Value
        End Set
    End Property

    <Bindable(True), Category("Upload"), DefaultValue(""), Localizable(True)> _
    Property MaxFileNumber() As Integer
        Get
            If IsNothing(ViewState("MaxFileNumber")) Then
                Return 5
            Else
                Return CInt(ViewState("MaxFileNumber"))
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("MaxFileNumber") = Value
        End Set
    End Property

    Property FileFilters() As List(Of Objects.FileFilter)
        Get
            If IsNothing(ViewState("FileFilters")) Then
                Return Nothing
            Else
                Return CType(ViewState("FileFilters"), List(Of Objects.FileFilter))
            End If
        End Get
        Set(ByVal value As List(Of Objects.FileFilter))
            ViewState("FileFilters") = value
        End Set
    End Property

#End Region

#Region "Subs"

    Public Sub New()
        Me.FileFilters = New List(Of Objects.FileFilter)
    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
        'Check required properties.
        If Me.UploadFolder.Trim.Length = 0 Or Not IO.Directory.Exists(Context.Server.MapPath(Me.UploadFolder)) Then
            Throw New ArgumentNullException("UploadFolder", My.Resources.ErrorValidPath)
        End If
        'Get handler.
        Dim handlerPath As String = Helpers.GetHandlerPath(GetType(UploaderHandler))
        'Set flash variables.
        Dim flashVars As String = String.Empty
        flashVars &= String.Format("serviceUrl={0}", Context.Server.UrlEncode(Me.ResolveClientUrl(handlerPath)))
        flashVars &= String.Format("&folderPath={0}", Context.Server.UrlEncode(Me.UploadFolder))
        flashVars &= String.Format("&maxFileSize={0}", Me.MaxFileSize)
        flashVars &= String.Format("&maxFileNumber={0}", Me.MaxFileNumber)
        'Set file filter.
        If Me.FileFilters.Count > 0 Then
            Dim filters As String = String.Empty
            For Each filter As Objects.FileFilter In Me.FileFilters
                filters &= String.Format("{0}|{1}||", filter.Title, filter.Extensions)
            Next
            If filters.EndsWith("||") Then filters = filters.Remove(filters.Length - 2, 2)
            flashVars &= String.Format("&fileFilters={0}", Context.Server.UrlEncode(filters))
        End If
        'Write control.
        writer.Write(String.Format(My.Resources.Flash, Me.ID, Me.FlashWidth, Me.FlashHeight, Me.ResolveClientUrl(Me.FlashFilePath), flashVars))
    End Sub

#End Region

End Class
